import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/Home'
import StatisticsPerson from '@/views/StatisticsPerson'
import StatisticsProject from '@/views/StatisticsProject'
import Manage from '@/views/Manage'
import Write from '@/views/Write'
import Login from '@/views/Login'
import People from '@/views/People'
import store from '@/store'
import { Message } from 'element-ui'

Vue.use(Router)

const router = new Router({
    base: '/effort/',
    routes: [{
            path: '/',
            redirect: 'login'
        },
        {
            path: '/login',
            name: 'Login',
            component: Login,
            meta: {
                title: "工时系统",
            }
        },
        {
            path: '/home',
            name: 'Home',
            component: Home,
            children: [{
                    path: './',
                    redirect: 'write'
                },
                {
                    path: 'person',
                    name: 'StatisticsPerson',
                    component: StatisticsPerson,
                    meta: {
                        title: "工时系统｜个人数据",
                        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
                    }
                },
                {
                    path: 'project',
                    name: 'StatisticsProject',
                    component: StatisticsProject,
                    meta: {
                        title: "工时系统｜项目数据",
                        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
                    }
                },
                {
                    path: 'people',
                    name: 'People',
                    component: People,
                    meta: {
                        title: "工时系统｜人员数据",
                        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
                    }
                },
                {
                    path: 'manage',
                    name: 'Manage',
                    component: Manage,
                    meta: {
                        title: "工时系统｜项目管理",
                        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
                    }
                },
                {
                    path: 'write',
                    name: 'Write',
                    component: Write,
                    meta: {
                        title: "工时系统｜工时填写",
                        requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
                    }
                }
            ],
            meta: {
                requireAuth: true // 添加该字段，表示进入这个路由是需要登录的
            }
        },
    ]
})

// 注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
    const token = store.state.token
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (token) { // 通过vuex state获取当前的token是否存在
            next()
            document.title = to.meta.title
        } else {
            Message({
                showClose: true,
                message: '您暂时未登录，请先登录',
                type: 'error'
            });
            next({
                path: '/login'
                    // query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
            })
        }
    } else {
        next()
        document.title = to.meta.title
    }
})

export default router;
